//
//  AppDelegate.m
//  UIA段项目 墨家天气
//
//  Created by dllo on 15/8/20.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "AppDelegate.h"
#import "WeatherViewController.h"
#import "LiveViewController.h"
#import "MyViewController.h"
#import "AddCity.h"
//#import "UMSocial.h"
@interface AppDelegate ()<UIScrollViewDelegate>

@end
BOOL isOut = NO;
@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
//    [UMSocialData setAppKey:@"55eae0e067e58ee28e0028e7"];
    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    [self.window makeKeyAndVisible];
    
    WeatherViewController *weather = [[WeatherViewController alloc]init];
    UINavigationController *wea = [[UINavigationController alloc]initWithRootViewController:weather];
    

    weather.tabBarItem =  [[UITabBarItem alloc]initWithTitle:@"天气" image:[UIImage imageNamed:@"tianqi.png"] tag:1];
    wea.navigationBar.hidden = YES;


    
    LiveViewController *live = [[LiveViewController alloc]init];
    live.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"实景" image:[UIImage imageNamed:@"shijing.png"] tag:2];
    
    
    MyViewController *my = [[MyViewController alloc]init];
    my.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"我" image:[UIImage imageNamed:@"wo.png"] tag:3];
    UINavigationController *mine = [[UINavigationController alloc]initWithRootViewController:my];
    mine.navigationBar.hidden = YES;
    

    UITabBarController *tap = [[UITabBarController alloc]init];
    tap.tabBar.barStyle = UIBarStyleBlack;

    tap.tabBar.backgroundColor = [UIColor blackColor];

    tap.viewControllers = @[wea,live,mine]  ;
    self.window.rootViewController = tap;

    
    /* 此处及以下代码是添加引导页 */
    
    if (![[NSUserDefaults standardUserDefaults]boolForKey:@"everLaunched"]) {
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"everLaunched"];
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"firstLaunch"];
    }else{
        [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"firstLaunch"];
    }
    
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"firstLaunch"])
    {
        [self makeLaunchView];
    }else{
        [self gotoMain];
    }
    
    
//    [self makeLaunchView];
    [_window release];
    return YES;
}


#if 1
- (void)makeLaunchView
{
    NSArray *arr=[NSArray arrayWithObjects:@"44.png",@"33.jpg", nil];//数组内存放的是我要显示的假引导页面图片
    //通过scrollView 将这些图片添加在上面，从而达到滚动这些图片
    UIScrollView *scr = [[UIScrollView alloc] initWithFrame:self.window.bounds];
    scr.contentSize = CGSizeMake(self.window.frame.size.width * arr.count + 120 * OffWidth, self.window.frame.size.height - 20);
    scr.pagingEnabled = YES;
    scr.tag = 7000;
    scr.delegate = self;
    scr.showsHorizontalScrollIndicator = NO;
    [self.window addSubview:scr];
    for (int i=0; i<arr.count; i++) {
        UIImageView *img=[[UIImageView alloc] initWithFrame:CGRectMake(i * self.window.frame.size.width, 0, self.window.frame.size.width, self.window.frame.size.height)];
        img.image=[UIImage imageNamed:arr[i]];
        [scr addSubview:img];
        [img release];
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //这里是在滚动的时候判断 我滚动到哪张图片了，如果滚动到了最后一张图片，那么
    //如果在往下面滑动的话就该进入到主界面了，我这里利用的是偏移量来判断的
    //一共五张图片，所以当图片全部滑完后 又向后多滑了30 的时候就做下一个动作
    if (scrollView.contentOffset.x > 1 * self.window.frame.size.width) {
        
        isOut=YES;//这是我声明的一个全局变量Bool 类型的，初始值为NO，当达到我需求的条件时将值改为YES
        
    }
}



- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    //判断isout为真 就要进入主界面了
    if (isOut) {
        //这里添加了一个动画，（可根据个人喜好）
        [UIView animateWithDuration:1 animations:^{
            scrollView.alpha = 0;//让scrollview 渐变消失
            scrollView.frame = CGRectMake(- self.window.frame.size.width, 0, self.window.frame.size.width, self.window.frame.size.height);
        }completion:^(BOOL finished) {
            
            //将scrollView移除
            [scrollView  removeFromSuperview];
            
            [self gotoMain];//进入主界面
            
        } ];
    }
    
}



- (void)gotoMain
{
    //如果第一次进入没有文件，那么就创建这个文件
    NSFileManager *manager=[NSFileManager defaultManager];
    
    //判断 我是否创建了文件，如果没创建 就创建这个文件（这种情况就运行一次，也就是第一次启动程序的时候）
    if (![manager fileExistsAtPath:[NSHomeDirectory() stringByAppendingString:@"aa.txt"]]) {
        [manager createFileAtPath:[NSHomeDirectory() stringByAppendingString:@"aa.txt"] contents:nil attributes:nil];
    }
}


#pragma mark -
#pragma mark — 清除缓存
- (long long)fileSizeAtPath:(NSString*) filePath
{
    NSFileManager* manager = [NSFileManager defaultManager];
    if ([manager fileExistsAtPath:filePath]){
        return [[manager attributesOfItemAtPath:filePath error:nil] fileSize];
    }
    return 0;
}


//遍历文件夹获得文件夹大小，返回多少
- (float )folderSizeAtPath:(NSString*) folderPath
{
    NSFileManager* manager = [NSFileManager defaultManager];
    if (![manager fileExistsAtPath:folderPath]) return 0;
    NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath:folderPath] objectEnumerator];
    NSString* fileName;
    long long folderSize = 0;
    while ((fileName = [childFilesEnumerator nextObject]) != nil){
        NSString* fileAbsolutePath = [folderPath stringByAppendingPathComponent:fileName];
        folderSize += [self fileSizeAtPath:fileAbsolutePath];
    }
    return folderSize / (1024.0 * 1024.0);
}
#endif







- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
